import {
	getList,
	deletepowerZone,
	editpowerZone,
	addpowerZone,
} from "../apis/powerZoneAPI";
import { ElMessage } from "element-plus";
import { ref, reactive } from "vue";
export const usePowerZone = () => {
	/**
	 * 加载状态
	 */
	const loading = ref(false);
	const searchForm = reactive({
		zoneName: '',
		totalCapacityMix: null,
		totalCapacityMax: null,
		status: null,
	});

	// 搜索
	const handleSearch = () => {
		pagination.currentPage = 1;
		LoadingPowerZoneList();
	};

	// 重置搜索
	const handleReset = () => {
		Object.assign(searchForm, {
			zoneName: '',
			totalCapacityMix: null,
			totalCapacityMax: null,
			status: null,
		});
		pagination.currentPage = 1;
		LoadingPowerZoneList();
	};

	// 分页信息
	const pagination = reactive({
		currentPage: 1,
		pageSize: 10,
		total: 0,
	});

	// 分页大小改变
	const handleSizeChange = (size) => {
		pagination.pageSize = size;
		LoadingPowerZoneList();
	};

	// 页码改变
	const handleCurrentChange = (current) => {
		pagination.currentPage = current;
		LoadingPowerZoneList();
	};

	// 新增对话框
	const addDialogVisible = ref(false);
	const addForm = reactive({
		zoneName: '',
		totalCapacity: 0.00,
		status: 0,
	});
	const addLoading = ref(false);

	// 打开新增对话框
	const handleAdd = () => {
		Object.assign(addForm, {
		zoneName: '',

		totalCapacity: 0.00,
		status: 0,
		});
		addDialogVisible.value = true;
	};
	// 保存新增
	const saveAdd = async () => {
		addLoading.value = true;
		try {
			const response = await addpowerZone(addForm);
			if (response.code === 200) {
				ElMessage.success("新增成功");
				addDialogVisible.value = false;
				LoadingPowerZoneList();
			} else {
				ElMessage.error(response.message || "新增失败");
			}
		} catch (error) {
			console.error("新增区域失败:", error);
			ElMessage.error("新增失败，请稍后重试");
		} finally {
			addLoading.value = false;
		}
	};

	// 客户列表
	const powerZoneList = ref([]);

	const LoadingPowerZoneList = async () => {
		loading.value = true;

		try {
			const params = {
				...searchForm,
				page: pagination.currentPage,
				pageSize: pagination.pageSize,
			};
			const { code, data, message } = await getList(params);
			if (code === 200) {
				powerZoneList.value = data.records;
				pagination.total = data.total;
			} else {
				ElMessage.error(message);
			}
		} catch (error) {
			console.error("加载客户列表失败:", error);
		} finally {
			loading.value = false;
		}
	};

	// 查看对话框
	const viewDialogVisible = ref(false);
	const viewForm = ref({});
	const handleView = (row) => {
		viewForm.value = row;
		viewDialogVisible.value = true;
	};

	// 删除功能
	const handleDelete = async (id) => {
		loading.value = true;
		try {
			await deletepowerZone(id);
			// 删除成功后，刷新区域列表
			ElMessage.success("删除成功");
			LoadingPowerZoneList();
		} catch (error) {
			console.error("删除区域失败:", error);
		} finally {
			loading.value = false;
		}
	};

	// 编辑功能
	const editDialogVisible = ref(false);

	const editForm = reactive({
		zoneName: '',
		totalCapacity: 0.00,
		status: 0,
	});
	const editLoading = ref(false);

	const handleEdit = (row) => {
		Object.assign(editForm, row);
		editDialogVisible.value = true;
	};

	const saveEdit = async () => {
		editLoading.value = true;
		try {
			await editpowerZone(editForm);
			ElMessage.success("编辑成功");
			editDialogVisible.value = false;
			LoadingPowerZoneList();
		} catch (error) {
			console.error("编辑区域失败:", error);
			ElMessage.error("编辑失败，请稍后重试");
		} finally {
			editLoading.value = false;
		}
	};
	// 格式化区域状态
	const formatStatus = (status) => {
		const statusMap = {
			0: "正常",
			1: "停用",
			2: "检修",
		};
		return statusMap[status] || "未知";
	};

	// 区域类型选项
	const powerZoneTypes = [
		{ label: '工业用电', value: 1 },
		{ label: '商业用电', value: 2 },
		{ label: '居民用电', value: 3 },
		{ label: '其他', value: 4 }
	];

	// 格式化区域类型
	const formatType = (type) => {
		const typeMap = {
			1: "工业用电",
			2: "商业用电",
			3: "居民用电",
			4: "其他"
		};
		return typeMap[type] || "未知";
	};

	return {
		loading,
		// 区域列表
		powerZoneList,
		LoadingPowerZoneList,
		// 查看客户详情
		viewDialogVisible,
		viewForm,
		handleView,
		// 删除客户
		handleDelete,
		// 编辑客户
		editDialogVisible,
		editForm,
		saveEdit,
		handleEdit,
		powerZoneTypes,
		editLoading,
		// 搜索表单
		searchForm,
		// 搜索
		handleSearch,
		// 重置搜索
		handleReset,

		// 分页信息
		pagination,
		// 分页切换
		handleCurrentChange,
		handleSizeChange,

		// 新增客户
		addDialogVisible,
		addForm,
		handleAdd,
		addLoading,
		saveAdd,
		// 格式化方法
		formatType,
		formatStatus,
	};
};
